package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
//aaa
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    @Override
    public List<NursingPlanVo> getAll() {
        return nursingPlanMapper.getAll();
    }

    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        nursingPlanMapper.add(nursingPlanDto);
        nursingProjectPlanMapper.add(projectPlans);
    }

    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.selectByPage(name,status);
        PageResponse<NursingPlanVo> pageResponse  = new PageResponse(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    @Override
    public NursingPlanVo getById(Long id) {


        NursingPlanVo nursingPlanVo = nursingPlanMapper.getById(id);
        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.getByProjectId(id);
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;
    }

    @Override
    @Transactional
    public void updateById(Long id, NursingPlanDto nursingPlanDto) {
        //1 放入新计划
        nursingPlanMapper.updateById(nursingPlanDto);
        //2 删除旧方案链接
        nursingProjectPlanMapper.deleteByPlanId(id);
        //3 加入新方案链接
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        nursingProjectPlanMapper.add(projectPlans);

    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        nursingProjectPlanMapper.deleteByPlanId(id);
        nursingPlanMapper.deleteById(id);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        status= status==0?1:0;
        nursingPlanMapper.changeStatus(id,status);
    }


}
